import top.malaoshi.data.DataColumn;
import top.malaoshi.data.DataQuery;
import top.malaoshi.data.DataRow;
import top.malaoshi.data.DataTable;
import top.malaoshi.jdbc.DbSession;
import top.malaoshi.jdbc.DbSessionFactory;
import top.malaoshi.jdbc.Record;
import top.malaoshi.util.Out;

import java.sql.SQLException;
import java.util.List;

public class S8连数据库测索引 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String url="jdbc:mysql://localhost:3308/malaoshi2?useSSL=false&useUnicode=true&characterEncoding=utf-8";

        DbSession session = DbSessionFactory.newSession("com.mysql.jdbc.Driver"
                , url
                ,"root"
                ,"");

//        String sql = "select money from account";
        String sql = "select id,title,content,modify_time,view_num from article";
//        List<bean.Account> list = session.query(bean.TestType.class,sql);
        List<Record> list = session.query(sql);


        DataTable dataTable = new DataTable();
        dataTable.columns().add("id", DataColumn.Type.TEXT);
        dataTable.columns().add("title", DataColumn.Type.TEXT);
        dataTable.columns().add("content", DataColumn.Type.TEXT);
        dataTable.columns().add("modify_time", DataColumn.Type.TEXT);
        dataTable.columns().add("view_num", DataColumn.Type.INT);

        dataTable.indexManager.addIndex("id");
        dataTable.indexManager.addIndex("view_num");

        for(int i = 0;i<1000;i++) {
            for (Record record : list) {
                dataTable.rows().add(new Object[]{
                        record.getValue("id"),
                        record.getValue("title"),
                        record.getValue("content"),
                        record.getValue("modify_time"),
                        record.getValue("view_num"),
                });
            }
        }
        Out.println("数据量：",dataTable.rows().count());
        long s = System.currentTimeMillis();
        for(int i=0;i<100;i++) {
            DataRow[] ids = dataTable.select("id", DataQuery.Option.eq, "1C711qUSVlu8");
//            Out.println("id:",ids[0].getValue("title"));
        }
        for(int i=0;i<100;i++) {
            DataRow[] ids = dataTable.select("view_num", DataQuery.Option.eq, 10112);
//            Out.println("id:",ids[0].getValue("title"));
        }
        long e = System.currentTimeMillis();
        Out.println("时间差：",(e - s)/1000.0);
    }

}
